#include "Channels.h"
#include "ChannelManager.h"
#include "Resource.h"
#include "String.h"
#include "SharedPtr.h"
#include "ResourceManager.h"
#include "Database/DatabaseEnv.h"
#include "StringConverter.h"
#include "NGLog.h"
#include "Character.h"
#include "CharManager.h"
#include "Items.h"
#include "ItemManager.h"
#include "WorldPacket.h"
#include "Towns.h"
#include "Effects.h"
#include "EffectManager.h"
#include "EffectModels.h"
#include "EffectModelManager.h"
#include "Users.h"
#include "UserManager.h"
#include "XMLData.h"
#include "Towns.h"
#include "Message.h"
#include "XMLData.h"
#include "OpcodesEx.h"
#include "VipCardManager.h"
#include "Tools.h"
#include "WSSocket.h"
#include "AIScript.h"

#include "showip.h"
#include "twmjn/TWMJOpcode.h"
#include "twmjn/TWMJAIInterface.h"
#include "twmjn/TWMJGameStruct.h"
#include "twmjn/TWMJXmlDataManager.h"
#include "twmjn/TWMJGameThreadLogic.h"

#include "GameDefine.h"
#include LOGIC_MGR_PATH
#include PROTOCOLS_MGR_PATH
#include XMLDATA_MGR_PATH
#include LOG_MGR_PATH


TWMJDealGameReadyQueueThread::TWMJDealGameReadyQueueThread(ResourcePtr town) : ThreadBase("TWMJDealGameReadyQueueThread")
{
}
//-----------------------------------------------------------------------
bool TWMJDealGameReadyQueueThread::run()
{
	while(ResourceManager::isWorldLoading()) 
	{ 
		sleep(2); 
	}


	while (true)
	{
		static int iii = 1;
		{
			if (iii++ == 1)
			{
			/*	unsigned int index_top = 0;
				unsigned int index_bottom = 0;
				fstream pfile("../scripts_xml/qqwry.dat");
				ofstream pfile2("../scripts_xml/qqwry2.dat", ostream::binary);
				if (pfile.is_open())
				{
					pfile.read((char *)&index_top, 4);
					pfile.read((char *)&index_bottom, 4);
					pfile.close();
					{
						for (unsigned int i = index_top + 28; i <= index_bottom; i += 7)
						{
							fstream pfile("../scripts_xml/qqwry.dat");
							pfile.seekg(i, pfile.beg);
							unsigned int ip = 0;
							pfile.read((char *)&ip, 4);
							if (ip != 0)
							{
								String result = "";
								result = sShowIP.getAddress(ip);
								pfile2 << ip << result;
							}
							pfile.close();
						}
					}
				}*/
			}
		}
		sleep(5000);
		std::list<ChannelPtr> list;
		sChannelMgr.getChannelsList(&list, 0, 10000, "ai = %d ", sXmlDataMgr.GetConfXMLValue("CENTER_BATTLE_CHANNEL_AI"));

		std::list<ChannelPtr>::iterator iter, ei = list.end();
		for (iter = list.begin(); iter != ei;iter++)
		{
			ChannelPtr pChan = *iter;
			if (pChan.isNull())
				continue;
			

			if (pChan->isWantRemove() == true)
			{
			//	(*iter)->setWantRemove();
				(*iter)->setUInt32Field("timeout", time(0));
				sResourceControlMgr.destroyResource((ResourceProxy)(*iter));
			}
		}
	}
	// if(m_pTown.isNull())
		// return true;
	
	// TownPtr pTown = (TownPtr)m_pTown;
	// TWMJAITownInterface * ati = TO_TOWN_INTERFACE(pTown->getAIInterface());
	// ProxyLockVecQueue & vecQueue = ati->GetReadyList();
	// uint32 queuesize = sXmlDataMgr.GetConfXMLValue("RQUEUE_NUM");	

	// std::set<ResourceProxy> *pSetChars = NULL;
	// while(true)
	// {
		// uint32 st = sXmlDataMgr.getTWMJconf("sleepTime");
		
		// Sleep(st);
		
		// //Log.Debug("channel fenpei","%d",sGLMgr.waitPlayerList.size());
		// std::vector<CharPtr>::iterator iter2 = sGLMgr.waitPlayerList.begin();
		// for(; iter2 != sGLMgr.waitPlayerList.end(); )
		// {
			// if((*iter2).isNull())
			// {
				// Log.Error("channel fenpei ","NULL CHRPTR");
				// iter2 = sGLMgr.waitPlayerList.erase(iter2);
				// continue;
			// }
			// //Log.Debug("channel fenpei","ID:%d",(*iter2)->getHandle());
			// //Log.Debug("channel fenpei","ID:%d",(*iter2)->getHandle());
			
			// TWMJAICreatureInterface * aiChrInter = TO_TWMJCREATURE_INTERFACE((*iter2)->getAIInterface());
			// //Log.Debug("channel fenpei","1");
			// if(!aiChrInter)
			// {
				// Log.Debug("joinTown","1");
				// iter2++;
				// continue;
			// }
			// //Log.Debug("channel fenpei","2");
			// if(aiChrInter->chr_state == TWMJAICreatureInterface::ready)
			// {
				// //Log.Debug("channel fenpei","3");
				// ChannelPtr pChn = aiChrInter->waitPtr;
				// if(pChn.isNull())
				// {
					// Log.Error("TWMJAITownInterface::joinTown","pChn is NULL");
					// aiChrInter->SetChannelNull();
					// iter2++;
					// continue;
				// }
				// TWMJAIChannelInterface* pChnAiInfe = TO_TWMJCHANNEL_INTERFACE(pChn->getAIInterface());
				// if(!pChnAiInfe)
				// {
					// Log.Error("TWMJAITownInterface::joinTown","pChnAiInfe is null");
					// aiChrInter->SetChannelNull();
					// iter2++;
					// continue;
				// }
		
				// if(pChnAiInfe->joinChannel(*iter2))
				// {
					// aiChrInter->chr_state = TWMJAICreatureInterface::fight;
					// aiChrInter->SetChannel((ResourcePtr)pChn);
					// if(!aiChrInter->GetChannelPtr().isNull())
					// Log.Debug("joinTown","setchannel ok");
					// iter2 = sGLMgr.waitPlayerList.erase(iter2);
				// }
				// else
					// iter2++;
			// }
			// else if(aiChrInter->chr_state == TWMJAICreatureInterface::missLink)
				// iter2 = sGLMgr.waitPlayerList.erase(iter2);
			// else 
				// ++iter2;
		// }
	// }

	return true;
}
